<!DOCTYPE html>
<html lang="zh-CN">

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>用setTimeout实现setInterval</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
  </style>
</head>

<body>
  <div></div>
</body>
<script>
  "use strict"; {
    // 使用递归的方式进行模拟
    let id = ""
    let interval = flag => {
      if (flag) {
        id = setTimeout(() => {
          console.log("111")
          interval(true)
        }, 1000)
      } else {
        clearTimeout(id)
      }
    }
    interval(true)
  }; {
    let interval = (func, t) => {
      let inside = () => {
        func()
        setTimeout(inside, t)
      }
      inside()
    }

    let like = () => {
      console.log("输出内容了")
    }

    interval(like, 1 * 1000)
  };
</script>

</html>